package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.TruckAnnexService;
import cn.huiyunche.base.service.mappers.CTruckCertMapper;
import cn.huiyunche.base.service.model.CTruckCert;
import cn.huiyunche.base.service.model.CTruckCertExample;
import cn.huiyunche.base.service.vo.TruckAnnexVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service

public class TruckAnnexServiceImpl implements TruckAnnexService {

    private static final Logger logger = LoggerFactory.getLogger(TruckAnnexServiceImpl.class);

    @Autowired
    private CTruckCertMapper cTruckCertMapper = null;

    private String save = "save";

    private String update = "update";

    @Override
    public List<TruckAnnexVo> queryTruckAnnexList(long truckId) throws Exception {
        logger.info("current class TruckAnnexServiceImpl method queryTruckAnnexList start ......");
        logger.info("truckId : " + truckId);
        CTruckCertExample search = new CTruckCertExample();
        CTruckCertExample.Criteria criteria = search.createCriteria();
        criteria.andTruckIdEqualTo(truckId);
        List<TruckAnnexVo> truckAnnexList = new ArrayList<TruckAnnexVo>();
        List<CTruckCert> cTruckCerts = new ArrayList<CTruckCert>();
        try {
            cTruckCerts = this.getCTruckCertMapper().selectByExample(search);
        } catch (Exception e) {
            logger.error("query truckAnnex error : {}", e);
            throw new BusinessException("查询车辆附件信息错误");
        }
        try {
            if (CollectionUtils.isNotEmpty(cTruckCerts) && cTruckCerts.size() > 0) {
                for (CTruckCert cTruckCert : cTruckCerts) {
                    TruckAnnexVo vo = new TruckAnnexVo();
                    BeanUtils.copyProperties(cTruckCert, vo);
                    truckAnnexList.add(vo);
                }
            }
        } catch (Exception e) {
            logger.error("TruckAnnex convert TruckAnnexVo error : {}", e);
            throw new BusinessException("Vo转换错误");
        }
        logger.info("current TruckAnnex size : " + truckAnnexList.size());
        logger.info("current class TruckAnnexServiceImpl method queryTruckAnnexList end ......");
        return truckAnnexList;
    }

    @Override
    public int add(CTruckCert cTruckCert) throws Exception {
        this.validation(cTruckCert, save);
        return this.getCTruckCertMapper().insertSelective(cTruckCert);
    }

    @Override
    public int update(CTruckCert cTruckCert) throws Exception {
        this.validation(cTruckCert, update);
        return this.getCTruckCertMapper().updateByPrimaryKeySelective(cTruckCert);
    }

    private void validation(CTruckCert cTruckCert, String flg) throws Exception {
        if (update.equals(flg)) {
            if (cTruckCert.getId() == null) {
                logger.info("id is not null");
                throw new BusinessException("主键不能为空");
            }
        }
        if (StringUtils.isBlank(cTruckCert.getPicType())) {
            logger.info("picType is not null");
            throw new BusinessException("图片编码不能为空");
        } else if (StringUtils.isBlank(cTruckCert.getPicDesc())) {
            logger.info("picDesc is not null");
            throw new BusinessException("图片描述不能为空");
        } else if (StringUtils.isBlank(cTruckCert.getPicKey())) {
            logger.info("picKey is not null");
            throw new BusinessException("图片Key不能为空");
        } else if (StringUtils.isBlank(cTruckCert.getPicItem())) {
            logger.info("picItem is not null");
            throw new BusinessException("图片类型不能为空");
        }
    }

    private CTruckCertMapper getCTruckCertMapper() {
        return this.cTruckCertMapper;
    }

    @Override
    public List<CTruckCert> loadPic(String truckId) throws Exception {
        logger.info("current class TruckAnnexServiceImpl method loadPic start ......");
        if (StringUtils.isEmpty(truckId)) {
            logger.info("truckid is not null");
            throw new BusinessException("车辆ID不能为空");
        }
        logger.info("truckId : " + truckId);
        CTruckCertExample search = new CTruckCertExample();
        CTruckCertExample.Criteria criteria = search.createCriteria();
        criteria.andTruckIdEqualTo(Long.parseLong(truckId));
        List<CTruckCert> truckAnnexs = new ArrayList<CTruckCert>();
        try {
            truckAnnexs = this.getCTruckCertMapper().selectByExample(search);
        } catch (Exception e) {
            logger.error("query truckAnnex error : {}", e);
            throw new BusinessException("查询车辆附件信息错误");
        }
        logger.info("current TruckAnnex size : " + truckAnnexs.size());
        logger.info("current class TruckAnnexServiceImpl method loadPic end ......");
        return truckAnnexs;
    }

    @Override
    public int del(Long truckId) throws Exception {
        if (truckId == null) {
            logger.info("truckid is not null");
            throw new BusinessException("用户ID不能为空");
        }
        CTruckCertExample example = new CTruckCertExample();
        example.createCriteria().andTruckIdEqualTo(truckId);
        return this.getCTruckCertMapper().deleteByExample(example);
    }

}
